package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.*;
import com.theo.service.EpidemicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/epidemic-info")
public class EpidemicInfoController {
    @Autowired
    private EpidemicInfoService epidemicInfoService;

    /***
     * 录入疫情数据
     * @param dailyEpidemicInfo
     * @param session
     * @return
     */
    @PostMapping("/ajax/input")
    @ResponseBody
    public AjaxResponseInfo inputData(@RequestBody DailyEpidemicInfo dailyEpidemicInfo, HttpSession session) {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        UserInfo user = (UserInfo) session.getAttribute("loginUser");
        if (user == null) {
            responseInfo.setCode(-2);
            responseInfo.setMsg("你还没有登录，无法操作");
        } else {
            System.out.println("要执行插入数据了");
            List<CityInfo> list = epidemicInfoService.saveEpidemicInfo(dailyEpidemicInfo);
            responseInfo.setData(list);
        }
        return responseInfo;
    }

    @GetMapping("/ajax/latestData")
    @ResponseBody
    public AjaxResponseInfo findLastestData() {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicDetailInfo> list = epidemicInfoService.findLastestData();
        responseInfo.setData(list);
        return responseInfo;
    }

    @GetMapping("/ajax/latestGlobalData")
    @ResponseBody
    public AjaxResponseInfo findLastestGlobalData() {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicDetailInfo> list = epidemicInfoService.findLastestGlobalData();
        responseInfo.setData(list);
        return responseInfo;
    }

    @GetMapping("/ajax/dataForChart")
    @ResponseBody
    public AjaxResponseInfo findDataForChart() throws ParseException {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicInfo> list = epidemicInfoService.selectDataForChart();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        responseInfo.setData(list);
        return responseInfo;
    }

    @GetMapping("/ajax/dataForGlobalChart")
    @ResponseBody
    public AjaxResponseInfo findDataForGlobalChart() throws ParseException {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicInfo> list = epidemicInfoService.selectDataForGlobalChart();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        responseInfo.setData(list);
        return responseInfo;
    }
    @GetMapping("/ajax/allData/{date}")
    @ResponseBody
    public AjaxResponseInfo findAllData(@PathVariable("date") String date) {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicDetailInfo> list = epidemicInfoService.findAllData(date);
        responseInfo.setData(list);
        return responseInfo;
    }

    @GetMapping("/ajax/allEpidemic")
    @ResponseBody
    public AjaxResponseInfo findAll() {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicInfo> list = epidemicInfoService.findAll();
        responseInfo.setData(list);
        return responseInfo;
    }

    @RequestMapping("/show.do")
    public String show(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<EpidemicInfo> list = epidemicInfoService.findAll();
        //list.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(list, 6);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/epidemic_list";

    }
    @GetMapping("/ajax/allByDate/{date}")
    @ResponseBody
    public AjaxResponseInfo findAllByDate(@PathVariable("date") String date) {
        AjaxResponseInfo responseInfo = new AjaxResponseInfo();
        List<EpidemicInfo> list = epidemicInfoService.findAllByDate(date);
        responseInfo.setData(list);
        return responseInfo;
    }

    @RequestMapping("/findAllByDate.do")
    public String findAllByDate(Model model,String date,@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 8);
        List<EpidemicInfo> list = epidemicInfoService.findAllByDate(date);
        //list.forEach(System.out::println);
        PageInfo pageInfo = new PageInfo(list, 6);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/epidemic_list";

    }

    @RequestMapping("/toUpdate.do")
    public String showUpdate(Model model, Integer id) {
        EpidemicInfo epidemicInfo = epidemicInfoService.queryEpidemicInfoById(id);
        model.addAttribute("Info", epidemicInfo);
        return "admin/epidemic_update";
    }

    @RequestMapping("/update.do")
    public String updateEpidemicInfo(Integer id) {
        return "epidemic";
    }

    @RequestMapping("/delete.do")
    public String deleteEpidemicInfoById(Integer id) {
        epidemicInfoService.deleteDataById(id);
        return "redirect:/epidemic-info/show.do";
    }
}

